<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: lh
 * Time: 2020/07/14
 * Desc: 红包应用
 */
class Controller_Pc_Api_V2_Envelope extends Controller_Pc_Api_V2_Base
{
    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Envelope
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Envelope
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('envelope', $this->request_params);

        // 服务
        $this->service = new Service_Api_Envelope();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 获取用户当前产品可用红包数量&列表
    public function action_get_enable_envelope()
    {
        try {
            $this->validation->get_enable_envelope()->check();

            $this->result['data'] = $this->service->get_enable_envelope($this->mid, $this->request_params['type_id']);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取会员的红包
    public function action_get_member_envelope()
    {
        try {
            $this->validation->get_member_envelope()->check();

            $this->result['data'] = $this->service->get_member_envelope($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取红包说明
    public function action_get_envelope_description()
    {
        try {
            $this->result['data'] = $this->service->get_envelope_description();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 支付成功获取订单的分享红包信息
    public function action_get_order_envelope_info()
    {
        try {
            $this->validation->get_order_envelope_info()->check();

            if (!St_Functions::is_normal_app_install('red_envelope')) {
                $this->result['data']['envelope_info'] = null;
            } else {
                $this->result['data'] = $this->service->get_order_envelope_info($this->request_params['order_sn']);
            }
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 领取红包
    public function action_receive_envelope()
    {

        try {
            $this->validation->receive_envelope()->check();

            $this->result['data'] = $this->service->receive_envelope($this->mid, $this->request_params['envelope_order_id']);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }
}